package com.teampower.idea.base;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {//DispatchAction
    
	private static final long serialVersionUID = 1L;
	protected final Logger logger = Logger.getLogger(this.getClass().getName());
	protected final Log log = LogFactory.getLog(getClass());
	protected String json;
	protected int start;//开始记录数
	protected int limit;//每页记录数
	
//	protected final ActionContext context = ActionContext.getContext();
//	protected final HttpServletRequest request = ServletActionContext.getRequest(); 
//	protected final HttpServletResponse response = ServletActionContext.getResponse(); 
//	protected final HttpSession session = (HttpSession) context.getSession();
	
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	/**
	 * 用途：返回Spring Bean工厂的一个Bean
	 * @param name
	 * @return
	 */
    public Object getBean(String name) {
    	ServletContext servletContext = ServletActionContext.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        return ctx.getBean(name);
    }

	/**
	 * response打印返回数据流
	 * @throws IOException 
	 */
    public void write() throws IOException {
    	HttpServletResponse response = ServletActionContext.getResponse();
    	response.setContentType("text/xml;charset=utf-8"); 
		PrintWriter printWriter = response.getWriter();
		printWriter.write(json);
		printWriter.flush();
		printWriter.close();
    }

}

